import {
  createRouter,
  createWebHistory,
  RouteRecordRaw,
  RouteMeta
} from 'vue-router'
import { useTitleEffect } from '@/effects/titleEffect'
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/user/login'
  },
  {
    path: '/next',
    component: () => import('@/views/NextTick/index.vue')
  },
  {
    path: '/user',
    component: () => import('@/layouts/blankLayout.vue'),
    children: [
      {
        path: 'login',
        component: () => import('@/views/User/login.vue')
      }
    ]
  },
  {
    path: '/spring',
    component: () => import('@/layouts/basicLayout.vue'),
    redirect: '/spring/rain',
    children: [
      {
        path: 'demo',
        component: () => import('@/views/demo/index.vue'),
        meta: {
          title: 'demo-测试'
        }
      },
      {
        path: 'mine',
        component: () => import('@/views/Mine/index.vue'),
        meta: {
          title: '我的'
        }
      },
      {
        path: 'popScience',
        component: () => import('@/views/PopScience/index.vue'),
        meta: {
          title: '科普'
        }
      },
      {
        path: 'shopMall',
        component: () => import('@/views/ShopMall/index.vue'),
        meta: {
          title: '商城'
        }
      },
      {
        path: 'rain',
        component: () => import('@/views/SpringRain/index.vue'),
        meta: {
          title: '春雨'
        }
      }
    ]
  }
]

// 导航守卫功能：

// 路由记录功能：
const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 设置移动端标题
router.beforeEach((to, from, next) => {
  console.log('from ', to.meta)
  useTitleEffect(to.meta)
  next()
})
console.log('router:', router)

export default router
